package com.xone.xapp;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import com.xone.refresh.library.PullToRefreshListView;

public class ProductListUserActivity extends BasicListActivity<ProductListUserHolder> {
	
	protected ProductUserAdapter productUserAdapter;
	protected PullToRefreshListView listView;
	protected String saleType;
	protected String title;
	public String getSaleType() {
		return saleType;
	}
	public void setSaleType(String saleType) {
		this.saleType = saleType;
	}
	@Override
	public int getContentView() {
		return R.layout.product_list_user;
	}
	@Override
	public String getHeaderTitle() {
		return title;
	}
	/**
	 * 列表视图
	 */
	@Override
	public PullToRefreshListView getListView() {
		return listView;
	}
	/**
	 * 列表适配器
	 */
	@Override
	public BasicListAdapter<ProductListUserHolder> getListAdapter(Context context,
			List<JSONObject> result) {
		return new ProductUserAdapter(context, result);
	}
	/**
	 * 列表的请求地址
	 */
	@Override
	public String getRequestURL() {
		return "/product/listJsonItemsForUser.html";
	}
	/**
	 * 列表的请求参数
	 */
	@Override
	public List<NameValuePair> getRequestParams() {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("product.saleType", saleType));
		return params;
	}
	/**
	 * 返回的请求中的json根列表名称
	 */
	@Override
	public String getJsonRootName() {
		return "list";
	}
	
	@SuppressWarnings("unchecked")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		Bundle bundle = this.getIntent().getExtras();
		if (null != bundle) {
			saleType = bundle.getString("saleType");
			if (StringUtils.isBlank(saleType)) {
				Toast.makeText(this, "无产品类型信息", Toast.LENGTH_SHORT).show();
				this.finish();
				return;
			}
		}
		if ("0".equals(saleType)) {
			title = "用户产品列表";
		} else if ("1".equals(saleType)) {
			title = "用户促销列表";
		} else if ("2".equals(saleType)) {
			title = "用户组团列表";
		}
		super.onCreate(savedInstanceState);
		if (!ContextUtils.isOnline(this)) {
			return;
		}
		if (!UserInfo.login) {
			startActivity(new Intent(this, LoginActivity.class));
			this.finish();
		}
		listView = (PullToRefreshListView) findViewById(R.id.product_list_user);
		showProgress();
		new InitDataTask(this, listView).execute(getRequestParams());
	}

}
